/**
 * <p>Title: Note.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-15 上午10:48:58  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.common.zm.cInterface.ce;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.zm.ce.domain.ZMCEWorkPlan;

/**
 * 工单接口
 * <p>Title: Note.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-15 上午10:48:58  张小飞  Create
 *</pre>
 */
public class Note {
	@SuppressWarnings("unused")
	private static final String _MAINTAIN_APPLICATION = "维修申请";
	private static final String _DEVICE_EXCEPTION = "异常";
	private static final String _WORK_PLAN = "工作计划";	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 
	 * <p>Function: getNoteBlock</p>
	 * <p>Description: 获取工单的block</p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-15上午10:55:17 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static EiBlock getNoteBlock(String blockName, Map params){                                                              
		EiBlock block = new EiBlock(blockName);
		
		EiColumn eiColumn = new EiColumn("fdKeyId");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdCode");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		List<Map> list = new ArrayList<Map>();
		list = dao.query("ZMCEEquipmentOrder.queryInterface", params);
		block.setRows(list);
		return block;
	}
	
	/**
	 * 工单完成时，修改计划或异常的状态
	 * 
	 * 参数noteRow是一条工单记录;
	 * <p>Function: completeNote</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-27上午10:06:57 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public static void completeNote(Map noteRow)
	{
		Object source = noteRow.get("fdSource");
		Object sourceId = noteRow.get("fdPlanId");
		
		if(_WORK_PLAN.equals(source))
		{
			planUpdate(sourceId, noteRow.get("fdTrueEndTime"));			
		}	
		else if(_DEVICE_EXCEPTION.equals(source))
		{
			unusualProblemUpdate(sourceId);
		}
	}
	
	/**
	 * 更新计划的状态
	 * <p>Function: planUpdate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-27上午10:08:25 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static void planUpdate(Object sourceId, Object trueEndTime)
	{
		Map map=new HashMap();
		map.put("fdKeyId", sourceId);			
		List<ZMCEWorkPlan> list=dao.query("ZMCEWorkPlan.query", map);
		//根据实际完成时间，判断计划完成时间，如果实际完成时间>计划完成时间，则修改计划中"是否延期"为"是"

		String sfyq="否";
		if(list.size() > 0)
		{
			map=new HashMap();
			map.put("fdPlanId", sourceId);	
			if(!(trueEndTime==null || "".equals(trueEndTime)))
			{
				map.put("fdTrueEndDate", trueEndTime.toString().substring(0, 10));
	            if(trueEndTime.toString().substring(0, 10).compareTo(list.get(0).getFdEndDate())>0){
	            	sfyq="是";
	            }
			}
            map.put("fdIsExtension", sfyq);
            map.put("fdStatus", "完成");
            dao.update("ZMCEWorkPlan.updateStatus", map);
            
            if(_DEVICE_EXCEPTION.equals(list.get(0).getFdSource()))
            {
            	unusualProblemUpdate(list.get(0).getFdSourceId());
            }
		}
	}
	
	/**
	 * 更新异常的状态
	 * <p>Function: unusualProblemUpdate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-27上午10:08:41 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static void unusualProblemUpdate(Object sourceId)
	{
    	Map inMap = new HashMap();
		inMap.put("fdKeyId", sourceId);
		inMap.put("fdStatus", "已处理");
		inMap.put("fdSolvePerson", UserSession.getInSessionProperty("UserCname"));
		inMap.put("fdSolveDatetime", StaticTools.getDateTime());
    	dao.update("ZMCEUnusualProblem.updateStatus",inMap);
    	
    	inMap.remove("fdStatus");
    	List list = dao.query("ZMCEUnusualProblem.queryInterface", inMap);
    	if(list.size() > 0)
    	{
    		if(((Map)list.get(0)).get("fdWorkRecordId")!=null && !"".equals(((Map)list.get(0)).get("fdWorkRecordId")))
    		{
				inMap = new HashMap();
				inMap.put("fdKeyId", ((Map)list.get(0)).get("fdWorkRecordId"));
				inMap.put("fdStatus", "已处理");
				dao.update("ZMCEFaultRecord.updateStatus", inMap);
    		}
    	}
	}
}
